package cn.tedu.csmall.passport.ex.handler;

import cn.tedu.csmall.passport.ex.ServiceException;
import cn.tedu.csmall.passport.web.JsonResult;
import cn.tedu.csmall.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    public GlobalExceptionHandler() {
        System.out.println("创建全局异常处理类：GlobalExceptionHandler");
    }

    @ExceptionHandler
    public JsonResult<Void> handleServiceException(ServiceException e){
        log.error("捕获到ServiceException异常：{}",e.getMessage());
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public String handlerNullPointerException(NullPointerException e){
        log.debug("捕获到NullPointerException异常：{}",e.getMessage());
        return e.getMessage();
    }

    @ExceptionHandler
    public String handlerRuntimeException(RuntimeException e){
        log.debug("捕获到RuntimeException异常：{}",e.getMessage());
        e.printStackTrace();
        return e.getMessage();
    }

    @ExceptionHandler
    public String handlerHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
        log.debug("捕获到HttpRequestMethodNotSupportedException：{}",e.getMethod());
        return "非法访问";
    }

    //用户名不存在，或密码错误，认证抛出的异常
    @ExceptionHandler({BadCredentialsException.class,InternalAuthenticationServiceException.class})
    public JsonResult<Void> handlerAuthenticationException(AuthenticationException e){
        log.error("捕获到AuthenticationException异常");
        log.error("异常类型：{}",e.getClass().getName());
        log.error("异常消息：{}",e.getMessage());
        String message = "登录失败，用户名或密码错误";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handlerDisabledException(DisabledException e){
        log.error("捕获到DisabledException异常：{}",e.getMessage());
        String message = "登录失败，该管理员账号已被禁用";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLED,message);
    }

    /*AccessDeniedException*/
    @ExceptionHandler
    public JsonResult<Void> handlerAccessDeniedException(AccessDeniedException e){
        log.error("捕获到AccessDeniedException异常：{}",e.getMessage());
        String message = "请求失败，该管理员账号权限不足";
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN,message);
    }

    @ExceptionHandler
    public String handlerThrowable(Throwable e){
        log.debug("捕获到Throwable异常：{}",e.getMessage());
        e.printStackTrace();//强烈建议，仅在开发过程中建议，因为会导致系统资源被占用
        return "未知错误，请联系管理员";
    }
}
